/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pl.swspiz;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.sql.*;

/**
 *
 * @author mpytasz
 */
public class CustomSelect extends SimpleTagSupport {
    private String nazwa;
    Connection currentCon = null;
    ResultSet rs = null;
    Statement stmt = null;
    /**
     * Called by the container to invoke this tag. 
     * The implementation of this method is provided by the tag library developer,
     * and handles all tag processing, body iteration, etc.
     */
    @Override
    public void doTag() throws JspException {
        JspWriter out = getJspContext().getOut();

        try {
            currentCon = ConnectionManager.getConnection();
            stmt = currentCon.createStatement();
            rs = stmt.executeQuery("Select * from jednostki;");
            out.println("");
            while (rs.next()) {
                out.println("<option value="+rs.getString("nazwa")+">"+rs.getString("nazwa")+"</option>");

            }
     
         
        } catch (Exception ex) {
            throw new JspException("Error in TabelkaWTagu tag", ex);
        } finally {
          if (rs != null)	{
	            try {
	                rs.close();
	            } catch (Exception e) {}
	            rs = null;
	        }

	        if (stmt != null) {
	            try {
	                stmt.close();
	            } catch (Exception e) {}
	            stmt = null;
	        }
          if (currentCon != null) {

	            try {
	                currentCon.close();
	            } catch (Exception e) {
	            }
	            currentCon = null;

	        }


        }
    }

    public void setNazwa(String nazwa) {
        this.nazwa = nazwa;
    }

}
